package com.ambow.ajax;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

public class JsonAjax  extends HttpServlet {

	private static final long serialVersionUID = -1211799529309616867L;

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String name=req.getParameter("name");
		String age=req.getParameter("age");
		System.out.println("hi "+name+":"+age);
		PrintWriter pw=resp.getWriter();
		List<Student> list=getList();
		JSONArray jsonArray = JSONArray.fromObject(list);  
        System.out.println(jsonArray.toString()); 	
        pw.write(jsonArray.toString());
		pw.flush();
		pw.close();
	}

	private List<Student> getList() {
		List<Student> list=new ArrayList<Student>();
		for(int i=0;i<10;i++){
			Student s=new Student();
			s.setId(i+1);
			s.setName("Test"+i);
			list.add(s);
		}
		return list;
	}
}
